package com.hntx.joymusic.service;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.blankj.utilcode.utils.ToastUtils;
import com.hntx.joymusic.mediaplayer.VideoPlayerPageActivity;
import com.hntx.joymusic.util.LiveDataBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class MPushService extends GrayService {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    Observer observer;

    @Override
    public void onCreate() {
        super.onCreate();
        observer = (Observer<Object>) object -> executeCommand((Map<String, String>) object);
        LiveDataBus.getInstance().with("serviceMsg", Object.class)
                .observeForever(observer);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LiveDataBus.getInstance().with("serviceMsg").removeObserver(observer);
    }

    private void executeCommand(Map<String, String> map) {
//        String command = map.get(getString(R.string.server_command));
//        final String extra = map.get(getString(R.string.server_extra));
//        if (TextUtils.isEmpty(command)) return;
        Map<String, String> sendmap = new HashMap<>();
//        switch (command) {
//            case PushCommand.START_PLAY:
//
//                break;
//            case PushCommand.LINESTATE: {
//                String hallId = getLocalId(R.string.client_mpush_username);
//                sendmap.put("hallId", hallId);
//                sendmap.put("command", "online");
//                sendmap.put(getString(R.string.msg_type), "4");
//                /**2018.6.21 jpush*/
//                WebScoketClient.getWebScoketClient().sendBack(sendmap);
//            }
//            break;
//            case PushCommand.UNLOCK: {
//                /**关闭锁屏页面*/
//                boolean flag = PropertiesUtil.updateLockStatus(getString(R.string.lock_status), "false");
//                sendmap.put("hallId", getLocalId(R.string.client_mpush_username));
//                if (!flag) {
//                    if (GoldLockActivity.instance != null) {
//                        GoldLockActivity.instance.finish();
//                    }
//                    sendmap.put("command", "unLock");
//                } else {
//                    sendmap.put("command", "lock");
//                }
//                sendmap.put(getString(R.string.msg_type), "4");
//                /**2018.6.21 jpush*/
//                WebScoketClient.getWebScoketClient().sendBack(sendmap);
//            }
//            break;
//            case PushCommand.LOCK: {
//                /**启动锁屏页面*/
//                boolean flag = PropertiesUtil.updateLockStatus(getString(R.string.lock_status), "true");
//                Intent intent = new Intent(this, GoldLockActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                sendmap.put("hallId", getLocalId(R.string.client_mpush_username));
//                if (flag) {
//                    VLCApplication.getAppContext().startActivity(intent);
//                    sendmap.put("command", "lock");
//                } else {
//                    sendmap.put("command", "unLock");
//                }
//                sendmap.put(getString(R.string.msg_type), "4");
//                /**2018.6.21 jpush*/
//                WebScoketClient.getWebScoketClient().sendBack(sendmap);
//            }
//            break;
//            case PushCommand.TIMERSTART: {
//                String StrTimeStamp = getString(R.string.time_stamp);
//                String timeStamp = map.get(StrTimeStamp);
//                sendmap.put("hallId", getLocalId(R.string.client_mpush_username));
//                if (timeStamp != null && Long.decode(timeStamp) > 0) {
//                    PropertiesUtil.updateTimeStamp(StrTimeStamp, timeStamp);
//                    sendmap.put("command", "timerStart");
//                    sendmap.put("orderFilmId", Common.orderFilmId);
//                } else {
//                    PropertiesUtil.updateTimeStamp(StrTimeStamp, "0");
//                    sendmap.put("command", "timerStartError");
//                }
//                sendmap.put(getString(R.string.msg_type), "4");
//                WebScoketClient.getWebScoketClient().sendBack(sendmap);
//            }
//            break;
//            case PushCommand.ADDTIME: {
//                String StrTimeStamp = getString(R.string.time_stamp);
//                String timeStamp = map.get(StrTimeStamp);
//                sendmap.put("hallId", getLocalId(R.string.client_mpush_username));
//                if (timeStamp != null && Long.decode(timeStamp) > 0) {
//                    PropertiesUtil.updateTimeStamp(StrTimeStamp, timeStamp);
//                    sendmap.put("command", "addTime");
//                } else {
//                    PropertiesUtil.updateTimeStamp(StrTimeStamp, "0");
//                    sendmap.put("command", "addTimerError");
//                }
//                sendmap.put(getString(R.string.msg_type), "4");
//                WebScoketClient.getWebScoketClient().sendBack(sendmap);
//            }
//            break;
//            case PushCommand.TIMERSTOP: {
//                PropertiesUtil.updateTimeStamp(getString(R.string.time_stamp), "0");
//                boolean flag = PropertiesUtil.updateLockStatus(getString(R.string.lock_status), "true");
//                Intent intent = new Intent(this, GoldLockActivity.class);
//                intent.putExtra("gold_title", true);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                sendmap.put("hallId", getLocalId(R.string.client_mpush_username));
//                if (flag) {
//                    Map<String, String> mapStop = new HashMap<>();
//                    mapStop.put(getString(R.string.server_command), PushCommand.STOP_PLAY);
//                    mapStop.put("timeStopCon", "1");
//                    RxBus.getInstance().send(mapStop);
//                    VLCApplication.getAppContext().startActivity(intent);
////                    hideTipWindow();
//                    sendmap.put("command", "timerStop");
//                } else {
//                    sendmap.put("command", "timrStopError");
//                }
//                sendmap.put(getString(R.string.msg_type), "4");
//                /**2018.6.21 jpush*/
//                WebScoketClient.getWebScoketClient().sendBack(sendmap);
//            }
//            break;
//            case PushCommand.VOLUME:
//                Map<String, String> volumeMap = new HashMap<>();
//                volumeMap.put(getString(R.string.hall_id), getLocalId(R.string.client_mpush_username));
//                volumeMap.put(getString(R.string.command), PushCommand.VOLUME);
//                int getCurrentVolume = 0;
//                String currentVolume = map.get(getString(R.string.volume));
//                if (mAudioManager != null) {
//                    if (!TextUtils.isEmpty(currentVolume)) {
//                        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//                        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Integer.parseInt(currentVolume), 0);
//                        showInfoWithVerticalBar(getString(R.string.volume) + "\n" + (int) ((Double.parseDouble(currentVolume) / (maxVolume * 1.0d)) * 100) + '%', 1000, Integer.parseInt(currentVolume), maxVolume);
//                    }
//                    getCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//                }
//                volumeMap.put(getString(R.string.current_volume), getCurrentVolume + "");
//                volumeMap.put(getString(R.string.msg_type), "4");
//                WebScoketClient.getWebScoketClient().sendBack(volumeMap);
//                break;
//            case PushCommand.TEXTPUSH:
//                createTipWindow(Integer.decode(map.get(getString(R.string.mercPushShowLocation))));
//                showTipsNum(map.get(getString(R.string.templateContent)), Integer.decode(map.get(getString(R.string.mercPushShowCount))));
//                break;
//            default:
//                break;
//        }
    }

    /**
     * 2019.9.9 添加 url
     */
    private String movieUrl = null;

    private void playWithUrl(String extra) {
        if (!TextUtils.isEmpty(extra)) {
            /**开始时发往后台的数据*/
//            RxBus.getInstance(getString(R.string.rxbusUpload)).send(getString(R.string.play_start));
//            /*最新2018.3.14*/
//            Common.domainIp = smbPlayUtils.strSubDoMainIp(extra);
//            String smbUrl = smbPlayUtils.returnStringPath(extra);
//            String movUrl = StrSpecialProUtils.SpecialProStr(smbUrl);
//            if (!TextUtils.isEmpty(Common.movieSubtitle)) {
//                movieUrl = movUrl;
//                new MyAsyncTask().execute();
//            } else
//                playingMedia(movUrl);
        } else {
            ToastUtils.showShortToast("此影片地址为空，请重新点播！");
        }
    }

    private void playingListMedia(List<String> list) {
        Intent intent = new Intent(this, VideoPlayerPageActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("urlList", (ArrayList<String>) list);
        intent.putExtra("data", bundle);
        startActivity(intent);
    }

    private void playingMedia(String url) {
        Intent intent = new Intent(this, VideoPlayerPageActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        intent.putExtra("data", bundle);
        startActivity(intent);
    }

    class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void[] objects) {
//            String subtitlePath = smbPlayUtils.returnStringPath(Common.movieSubtitle);
//            return SubtitleDownUtils.download(subtitlePath);
            return null;
        }

        @Override
        protected void onPostExecute(Boolean flag) {
            super.onPostExecute(flag);
//            if (!flag) {
//                Common.movieSubtitle = null;
//            } else {
//                Common.movieSubtitle = SubtitleDownUtils.StrEndUpdate(Common.movieSubtitle);
//            }
//            if (handleList.size() > 0) {
//                playingListMedia(handleList);
//            } else {
//                playingMedia(movieUrl);
//            }
        }
    }

}
